<?php
require_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR ."core".DIRECTORY_SEPARATOR."functions.php");
core::getResource("Model.Event");

class ActivityController extends baseController
{
    private $activityId;
    private $activity;
    public function __construct($activityId)
    {
        $this->activityId = $activityId;
        $this->activity = new Activity($activityId);
    }

    public function getTitle()
    {
        return $this->activity->title;
    }
    
    public function getDescription()
    {
        return $this->activity->description;
    }
    
    public function getEventId()
    {
        return $this->activity->event;
    }
    
    public function getEventName()
    {
        return (new Event((int) $this->activity->event))->eventName;
    }
    
    public function getStartAndEndTimes()
    {
        return $this->activity->startTime." to ".$this->activity->endTime;
    }

    public function getStartAndEndDates()
    {
        return $this->activity->startDate." to ".$this->activity->endDate;
    }
    
    public static function getAllActivities($startDate)
    {
        return Activity::getActivitiesByDate($startDate);
    }
    
    public function activityExists()
    {
        if(Activity::exists($this->activityId))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public function getRoles()
    {
        return $this->activity->getRoles();
    }

    public static function canDelete($id)
    {
        $db = new dbConn();
        $res = $db->query("SELECT * FROM role WHERE activity=$id");
        if($db->num_rows($res)>0)
        {
            return false;
        }
        else
        {
            return true;
        }   
        
    }

    public static function delete($id)
    {
        $db = new dbConn();
        $data = $db->query("SELECT roleId FROM role JOIN activity ON role.activity = activity.activityId WHERE role.activity=$id");
        while($row = $db->fetch_array($data))
        {
            RoleController::delete($row['roleId']);
        }
        $db->query("DELETE FROM activity WHERE activityId=$id");
    }
}

?>